﻿using DotNetty.Transport.Channels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace socketAssist
{
    public partial class TCPSampler : Form
    {
        public TCPSampler()
        {
            InitializeComponent();
        }

        private List<IChannel> taskList = new List<IChannel>();
        private async void btn_run_Click(object sender, EventArgs e)
        {
            int connections = int.Parse(txt_connects.Text);
            int loopNums = int.Parse(txt_loop.Text);
            String ip = txt_ip.Text;
            int port = int.Parse(txt_port.Text);
            String msg = txt_send_text.Text;

            //List<IChannel> taskList = new List<IChannel>();
            DotNettyClient dotNettyClient = new DotNettyClient();
            //List<Task<IChannel>> tasks = new List<Task<IChannel>>();
            for(int i = 0; i < connections; i++)
            {
                var channel =await dotNettyClient.start(ip, port);
                taskList.Add(channel);
            }

            //Task.WaitAll(tasks.ToArray());

            if (!String.IsNullOrWhiteSpace(txt_send_text.Text))
            {
                Parallel.ForEach(taskList, (channel) =>
                {
                    channel.WriteAndFlushAsync(msg);
                });
            }
        }

        private async void btn_stop_Click(object sender, EventArgs e)
        {
            foreach(var channel in taskList)
            {
                await channel.CloseAsync();
            }
        }
    }
}
